/**
 * @Description	: Plugin to tweet about an item from within DC
 * @Author Miztiik
 * @Version 2.0
 * @Date 29Nov2010
 */
package plugins.iTw3et;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import net.datacrow.console.ComponentFactory;
import net.datacrow.console.components.DcButton;
import net.datacrow.console.components.DcTextPane;
import net.datacrow.console.windows.DcDialog;
import net.datacrow.core.data.DataManager;
import net.datacrow.core.objects.DcObject;
import net.datacrow.core.objects.DcTemplate;
import net.datacrow.core.objects.DcValue;
import net.datacrow.core.plugin.Plugin;

import twitter4j.TwitterException;

@SuppressWarnings("serial")
public class Tw3etMe extends Plugin {

	private ImageIcon twitterLogo;
	private DcObject DcObj;
	private Map<Integer, DcValue> DcObjValues = new HashMap<Integer, DcValue>();
	private Object value = null;
	
	// private int[] SelectedItemFieldIndices=null;
	// private Map<Integer, DcValue> values = new HashMap<Integer, DcValue>();

	public Tw3etMe(DcObject dco, DcTemplate template, int viewIdx, int moduleIdx) {
		super(dco, template, viewIdx, moduleIdx);

	}

	@Override
	public String getLabel() {
		return "TweetMe";
	}

	@Override
	public boolean isShowInMenu() {
		return true;
	}

	@Override
	public ImageIcon getIcon() {
		ImageIcon Twittericon = new ImageIcon("plugins/iTw3et/images/tw3et16.png", "Tweetie");
		return Twittericon;
	}

	/**
	 * @Description ActionPerformed method for plugin, Createds the dialog and sets in properties and components. Overrides the method in plugin class
	 */
	@Override
	public void actionPerformed(ActionEvent ae) {
		DcDialog dialog = new DcDialog();
		dialog.setTitle("Tweet About this Item");
		dialog.getContentPane().setLayout(new GridLayout(1, 2));

		DcTextPane usrTweetTxt = ComponentFactory.getTextPane();
		dialog.getContentPane().add(usrTweetTxt);
		
		twitterLogo = new ImageIcon("plugins/iTw3et/images/full_logo_blue.png");
		DcButton submitTweet = ComponentFactory.getButton(twitterLogo);
		submitTweet.addActionListener(submitTwitter(usrTweetTxt));
		dialog.getContentPane().add(submitTweet);

		dialog.setResizable(false);
		dialog.pack();
		dialog.setSize(new Dimension(425, 80));
		dialog.setCenteredLocation();
		dialog.setModal(false);
		dialog.setFocusable(true);
		dialog.setVisible(true);
		
		int[] fields = null;
		DcObj= getItem();
		DcObjValues = DcObj.getValues();
		
		//DcObject dco = DataManager.getObject(DcModules,_SOFTWARE,"1");
		//DcObject dco = DataManager.getItem(54, "Authors", fields);
		//JOptionPane.showMessageDialog(null,dco.getField(2).toString());
		
	}

	/**
	 * @Description Actionlistener for the tweet button
	 * @return A new Actionlistener for the tweet button event
	 * @param Takes a Datacrow textpane element and reads its contents.
	 */
	private ActionListener submitTwitter(final DcTextPane usrTweetTxt) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				OAuthTwitterClient tw3etUpdateAgent = new OAuthTwitterClient();
				String updateResp,Tw3etStatus = null;
				Tw3etStatus = usrTweetTxt.getText();
				//JOptionPane.showMessageDialog(null,"Your Status not yet updated to: " + Tw3etStatus + " : Is there an empty space ?");
				try {
					updateResp = tw3etUpdateAgent.updateStatus(215323632,Tw3etStatus);
					// updateResp = tw3etUpdateAgent.updateStatus("Finally Got it!!!!!!Having problems with first time start");
					JOptionPane.showMessageDialog(null, "Your twitter status updated to : "+ updateResp);
				} catch (TwitterException ex) {
					Logger.getLogger(Tw3etMe.class.getName()).log(Level.SEVERE,null, ex);
				} catch (IOException ex) {
					Logger.getLogger(Tw3etMe.class.getName()).log(Level.SEVERE,null, ex);
				}
		  }
	};
	}
}